package io.zhengsh.jvm.ref;

import java.util.HashMap;
import java.util.WeakHashMap;

public class WeekHashMapDemo {
    public static void main(String[] args) {
        myHashMap();
        System.out.println("=========");
        myWeekHashMap();
    }

    private static void myWeekHashMap() {
        WeakHashMap<Integer, String> map = new WeakHashMap<>();
        Integer key = new Integer(2);
        String value = "WeakHashMap";

        map.put(key, value);
        System.out.println(map);

        key = null;
        System.out.println(map);


        System.gc();
        System.out.println(map + " \t" + map.size());
    }

    private static void myHashMap() {
        HashMap<Integer, String> map = new HashMap<>();
        Integer key = new Integer(1);
        String value = "HashMap";

        map.put(key, value);
        System.out.println(map);
        
        key = null;
        System.out.println(map);
        
        
        System.gc();
        System.out.println(map + " \t" + map.size());
    }
}
